package com.whut.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.whut.common.Message;
import com.whut.common.UserUtil;
import com.whut.common.JosnUtil;
import com.whut.entity.Courier;
import com.whut.entity.Express;
import com.whut.entity.User;
import com.whut.service.CourierService;
import com.whut.service.ExpressService;
import com.whut.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/wx")
public class WxExpressController {
    @Autowired
    private ExpressService expressService;

    @Autowired
    private UserService userService;

    @Autowired
    private CourierService courierService;

    /**
     * 根据手机号获取快递
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @GetMapping("/findExpressByUserPhone.do")
    public Message findByUserPhone(HttpServletRequest request, HttpServletResponse response){
        //获取当前用户ID
//        Long currentId = BaseContext.getCurrentId();
        /**
         * 模拟一下获取ID,因为还未合并登录功能
         */
        Long currentId = Long.valueOf(2);

        User wxUser = userService.getById(currentId);
        //获取当前用户手机号
        String userPhone = wxUser.getUserPhone();
        //构造条件构造器
        LambdaQueryWrapper<Express> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Express::getPhone, userPhone);

        //获取全部快递列表
        List<Express> list = expressService.list(queryWrapper);

        Message msg = new Message();
        Integer status = list.size() == 0 ? -1 : 0;
        msg.setStatus(status);

        //获取待取件快递列表
        queryWrapper.eq(Express::getStatus, 3);
        List<Express> status3List = expressService.list(queryWrapper);

        //构造条件构造器
        LambdaQueryWrapper<Express> expressLambdaQueryWrapper = new LambdaQueryWrapper<>();
        expressLambdaQueryWrapper.eq(Express::getPhone, userPhone);
        expressLambdaQueryWrapper.eq(Express::getStatus, 4);

        //获取已取件快递列表
        List<Express> status4List = expressService.list(expressLambdaQueryWrapper);

        Map data = new HashMap<>();
        data.put("status0",status3List);
        data.put("status1",status4List);
        msg.setData(data);

        return msg;
    }

    /**
     * 根据快递单号查询快递
     * @param request
     * @return
     */
    @PostMapping("/indexQueryExpress.do")
    public Message<Express> indexQueryExpress(HttpServletRequest request){
        // 快递单号
        String expressNum = request.getParameter("expressNum");
        // 用户 / 快递员
        Object wxUser = UserUtil.getWxUser(request.getSession());
        // 手机号码
        String userPhone = "";
        // 如果是用户
        if (wxUser instanceof User){
            userPhone = ((User) wxUser).getUserPhone();
        }
        // 如果是快递员
        else if (wxUser instanceof Courier){
            userPhone = ((Courier) wxUser).getSysPhone();
        }

        // 根据快递单号获取快递
        LambdaQueryWrapper<Express> queryWrapper = new LambdaQueryWrapper<>();
        // 添加条件 根据expressNum进行模糊查询
        queryWrapper.eq(expressNum!=null, Express::getNumber, expressNum);
        Express express = expressService.getOne(queryWrapper);

        System.out.println(express);

        // 根据快递单号查找到快递 并且 用户号码正确
        if (express != null && express.getPhone().equals(userPhone)){
            HttpSession session = request.getSession();
            session.setAttribute("searchExpress",express);
            return Message.success(express);
        }
        return Message.error("未查询到该快递");
    }

    /**
     * 修改快递状态

     */
    @GetMapping("/updateStatus.do")
    public String updateStatus(HttpServletRequest request){
        String number = request.getParameter("number");

        //构造条件构造器
        LambdaUpdateWrapper<Express> updateWrapper = new LambdaUpdateWrapper<>();
        //添加过滤条件
        //updateWrapper.set(Express::getStatus,status).in(Express::getNumber,number);
        updateWrapper.eq(Express::getNumber,number);
        updateWrapper.set(Express::getStatus,4);

        boolean flag = expressService.update(updateWrapper);

        Message msg = new Message();
        if(flag){
            msg.setStatus(0);
            msg.setResult("修改成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("修改失败");
        }
        String json = JosnUtil.toJSON(msg);
        return json;
    }
}
